package com.hanhai.material.scheduling.controller;

import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.hanhai.material.scheduling.entity.SysTimePeriod;
import com.hanhai.material.scheduling.service.SysTimePeriodService;
import org.springframework.web.bind.annotation.RestController;
import java.io.Serializable;
import java.util.List;

import static com.hanhai.material.scheduling.entity.table.SysTimePeriodTableDef.SYS_TIME_PERIOD;
import static com.mybatisflex.core.query.QueryMethods.distinct;

/**
 *  控制层。
 *
 * @author youchuang
 * @since 2024/04/18 15:42:14
 */
@RestController
@RequestMapping("/time_period")
public class SysTimePeriodController {

    @Autowired
    SysTimePeriodService sysTimePeriodService;

    @GetMapping("/list")
    public Result<?> getTimeList(){
        QueryWrapper queryWrapper=QueryWrapper.create();
        queryWrapper.select(distinct(SYS_TIME_PERIOD.SHIFT_NAME));
        List<SysTimePeriod> sysTimePeriodList=this.sysTimePeriodService.list(queryWrapper);
        for(SysTimePeriod timePeriod:sysTimePeriodList){
            queryWrapper=QueryWrapper.create();
            queryWrapper.where(SYS_TIME_PERIOD.SHIFT_NAME.eq(timePeriod.getShiftName()));
            queryWrapper.orderBy(SYS_TIME_PERIOD.ORDER_NO,true);
            List<SysTimePeriod> timePeriodList=this.sysTimePeriodService.list(queryWrapper);
            timePeriod.setTimePeriodList(timePeriodList);
        }
        return sysTimePeriodList.isEmpty()? ResultTool.fail():ResultTool.success(sysTimePeriodList);
    }
}
